package com.hu.lsp.config.slave;

import com.atomikos.jdbc.AtomikosDataSourceBean;
import com.baomidou.mybatisplus.core.MybatisConfiguration;
import com.baomidou.mybatisplus.extension.plugins.PaginationInterceptor;
import com.baomidou.mybatisplus.extension.spring.MybatisSqlSessionFactoryBean;
import com.mysql.jdbc.jdbc2.optional.MysqlXADataSource;
import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionTemplate;
import org.mybatis.spring.annotation.MapperScan;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;

import javax.sql.DataSource;
import java.sql.SQLException;

/**
 * @Author lsp
 * @Date 2020/5/13 14:51
 * @Version 1.0
 */
@Configuration
@MapperScan(value = "com.hu.lsp.dao.slave",sqlSessionTemplateRef = "slaveSqlSessionTemplate",sqlSessionFactoryRef  = "slaveSqlSessionFactory")
public class SlaveDatasourceConfig {
    private static final Logger log = LoggerFactory.getLogger(SlaveDatasourceConfig.class);

    /**
     * 创建DataSource
     * @return
     */
    @Bean("slaveDataSource")
    //@ConfigurationProperties(prefix = "spring.datasource.druid.slave")
    public DataSource slaveDataSource(SlaveConfig slaveConfig) throws SQLException {
        log.info("slaveDataSource{}",slaveConfig);
        MysqlXADataSource mysqlXaDataSource = new MysqlXADataSource();
        mysqlXaDataSource.setUrl(slaveConfig.getUrl());
        mysqlXaDataSource.setPinGlobalTxToPhysicalConnection(true);
        mysqlXaDataSource.setPassword(slaveConfig.getPassword());
        mysqlXaDataSource.setUser(slaveConfig.getUsername());
        mysqlXaDataSource.setPinGlobalTxToPhysicalConnection(true);
        //注册到全局事务
        AtomikosDataSourceBean xaDataSource = new AtomikosDataSourceBean();
        xaDataSource.setXaDataSource(mysqlXaDataSource);
        xaDataSource.setUniqueResourceName(slaveConfig.getUniqueResourceName());
        xaDataSource.setMinPoolSize(slaveConfig.getMinPoolSize());
        xaDataSource.setMaxPoolSize(slaveConfig.getMaxPoolSize());
        xaDataSource.setMaxLifetime(slaveConfig.getMaxLifetime());
        xaDataSource.setBorrowConnectionTimeout(slaveConfig.getBorrowConnectionTimeout());
        xaDataSource.setLoginTimeout(slaveConfig.getLoginTimeout());
        xaDataSource.setMaintenanceInterval(slaveConfig.getMaintenanceInterval());
        xaDataSource.setMaxIdleTime(slaveConfig.getMaxIdleTime());
        xaDataSource.setTestQuery(slaveConfig.getTestQuery());
        return xaDataSource;
    }

    /**
     * 创建sqlSessionFactory
     * @param dataSource
     * @return
     * @throws Exception
     */
    @Bean(name = "slaveSqlSessionFactory")
    public SqlSessionFactory slaveSqlSessionFactory(@Qualifier("slaveDataSource") DataSource dataSource) throws Exception {
       // SqlSessionFactoryBean sqlSessionFactoryBean = new SqlSessionFactoryBean();

        //分页插件
        MybatisConfiguration configuration = new MybatisConfiguration();
        configuration.addInterceptor(new PaginationInterceptor());
        MybatisSqlSessionFactoryBean sqlSessionFactoryBean = new MybatisSqlSessionFactoryBean();
        sqlSessionFactoryBean.setDataSource(dataSource);
        sqlSessionFactoryBean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath:mapper/*Mapper.xml"));
        return sqlSessionFactoryBean.getObject();
    }

    /**
     * 创建事务管理器
     * @param dataSource
     * @return
     */
 /*   @Bean(name = "slaveTransactionManager")
    public DataSourceTransactionManager slaveTransactionManager(@Qualifier("slaveDataSource") DataSource dataSource) {
        return new DataSourceTransactionManager(dataSource);
    }*/
    /**
     * 创建sqlSession模板
     * @param sqlSessionFactory
     * @return
     */
    @Bean(name = "slaveSqlSessionTemplate")
    public SqlSessionTemplate slaveSqlSessionTemplate(@Qualifier("slaveSqlSessionFactory") SqlSessionFactory sqlSessionFactory) {
        return new SqlSessionTemplate(sqlSessionFactory);
    }

}
